﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MedicWeb.MedicService;

namespace MedicWeb.forms {
    public partial class Delete : SessionPage {
        protected void Page_Load(object sender, EventArgs e) {
            var sid = Request.QueryString["id"];
            var sclass = Request.QueryString["class"];
            if (!string.IsNullOrEmpty(sid) && !string.IsNullOrEmpty(sclass)) {
                try {
                    int id = Convert.ToInt32(sid);
                    new MainService().Delete(GetSession(), sclass, id);
                    List<Doctor> doctorList = Session["docList"] as List<Doctor>;
                    Doctor toDel = null;
                    if (doctorList != null) {
                        foreach (var doctor in doctorList) {
                            if (doctor.Id == id) {
                                toDel = doctor;
                                break;
                            }
                        }
                    }
                    if (toDel != null) {
                        doctorList.Remove(toDel);
                        Session["docList"] = doctorList;
                    }
                    Session["success"] = "Удаление произведено успешно";
                } catch (Exception ex) {
                    Session["error"] = "Ошибка при удалении<br/>"+ex.Message;
                }
            } else {
                Session["error"] = "Удаление невозможно";
            }
        }
    }
}